(function( $, undefined ) {
	itrSkuId = 4;
	itrServiceId = 4;
	$( "table" ).each(function( index ) {
		  console.log( index + "table");
		});
	$.fn.calcInvoice = function() {
		  subTotal= parseFloat("0");
		  discountTotal= parseFloat("0");
		$( "table.invoice-table tbody tr" ).each(function( index ) {
			  var discountVal = ( $(this).find(".discount").val() * $(this).find(".price").val() ) / 100 ;
			  console.log(discountVal + " disvount");
			  var tax1Val = ( $(this).find(".tax1").val() * $(this).find(".price").val() ) / 100 ;
			  var tax2Val = ( $(this).find(".tax2").val() * $(this).find(".price").val() ) / 100 ;
			  console.log(tax1Val + " tax1Val");
			  console.log(tax2Val + " tax2Val");
			  var realItemPrice = $(this).find(".price").val() - discountVal + tax1Val + tax2Val;
			  console.log($(this).find(".price").val() + " SkuPrice");
			  console.log(realItemPrice + " realPrice");
			  var linePrice = $(this).find(".Qty").val() * realItemPrice;
			  console.log(linePrice + " linePrice");
			  var str = new String(linePrice);
			  var parts = str.split(".", 2);
			  if(new String(parts[1])>0)
				  var frac = new String(parts[1]).substr(0,2);
			  else
				  var frac= "00";
			  //console.log(parts[0] + " farc " + frac);
			  $(this).find(".lineTotal").text(parts[0] + "." + frac);
			  subTotal += parseFloat(linePrice);
			  console.log(subTotal + " subTotal");
			  if(subTotal>0){
				  $(".invoiceTotal").text(subTotal);
				  $("#invoiceTotal").val(subTotal);
			  }
			  discountTotal += (parseFloat(discountVal) * parseFloat($(this).find(".Qty").val()));
			  console.log(parseFloat(discountTotal) + " discountTotal");
			  if(discountTotal>0){
				  $("#discountTotal").val(discountTotal);
			  }

			  console.log($(".invoiceTotal").text());
			});
	};
	$.fn.addLine = function() {
	    var args = arguments[0] || {};
	    var targetClass = args.targetClass;
	    if(targetClass== "service"){
			$.get('/sales/s/invoiceServiceLine', function(data) {
				  $('.invoice-tables .' + targetClass + ' tbody').append(data);
				  var tr = $( '.invoice-tables .' + targetClass + ' tbody tr:last-child, .invoice-tables .' + targetClass + ' tbody tr:last-child input, .invoice-tables .' + targetClass + ' tbody tr:last-child select, .invoice-tables .' + targetClass + ' tbody tr:last-child span');
				  tr.find(".service").attr("name", "serviceProduct"+itrServiceId);
				  tr.find("input").each(function(index){
					  var inputNameStr = new String($(this).attr("name"));
					  var inputSubName = inputNameStr.split("3");
					  var inputNewName = inputSubName[0] + itrServiceId;
					  $(this).attr("name", inputNewName);
				  });
					var taxSelCont ="";
					for(optTaxIndex=0; optTaxIndex<taxIdList.length; optTaxIndex++){
						taxSelCont+= "<option value='" + taxIdList[optTaxIndex] + "'>" + taxNameList[optTaxIndex] + "</option>";
					}
					  tr.find(".tax1").html(taxSelCont);
					  tr.find(".tax2").html(taxSelCont);
					  tr.find(".tax1").attr("name", "serviceTaxOne"+itrSkuId);
					  tr.find(".tax2").attr("name", "serviceTaxTwo"+itrSkuId);
				  tr.find(".service").attr("name", "serviceProduct"+itrServiceId);
				  $("#serviceTrCount").attr("value", itrServiceId);
				  itrServiceId++;
				  tr.effect( "highlight", 2000 );
				});
	    }

	    if(targetClass== "prod"){
		$.get('/sales/s/invoiceLine', function(data) {
			  $('.invoice-tables .' + targetClass + ' tbody').append(data);
			  var tr = $( '.invoice-tables .' + targetClass + ' tbody tr:last-child, .invoice-tables .' + targetClass + ' tbody tr:last-child input, .invoice-tables .' + targetClass + ' tbody tr:last-child select, .invoice-tables .' + targetClass + ' tbody tr:last-child span');
			  tr.find("input").each(function(index){
				  var inputNameStr = new String($(this).attr("name"));
				  var inputSubName = inputNameStr.split("3");
				  var inputNewName = inputSubName[0] + itrSkuId;
				  $(this).attr("name", inputNewName);
			  });
			  var selCont ="";
				var taxSelCont ="";
//				for(var optIndex=0; optIndex<skuNameList.length; optIndex++){
//					selCont += "<option value='" + skuIdList[optIndex] + "'>" + skuNameList[optIndex] + "</option>";
//				}
				for(optTaxIndex=0; optTaxIndex<taxIdList.length; optTaxIndex++){
					taxSelCont+= "<option value='" + taxIdList[optTaxIndex] + "'>" + taxNameList[optTaxIndex] + "</option>";
				}
				  tr.find(".skuSelect").html(selCont);
				  tr.find(".tax1").html(taxSelCont);
				  tr.find(".tax2").html(taxSelCont);
				  tr.find(".skuSelect").attr("name", "skuProduct"+itrSkuId);
				  tr.find(".tax1").attr("name", "skuTaxOne"+itrSkuId);
				  tr.find(".tax2").attr("name", "skuTaxTwo"+itrSkuId);
				  $("#skuTrCount").attr("value", itrSkuId);
				  itrSkuId++;
			  tr.effect( "highlight", 2000 );
			});
	    }
	 };
})(jQuery);

$(document).ready(function(){
	$(".price, .discount, .Qty, .tax1, .tax2").change(function(){
		$.fn.calcInvoice();
	});
	$("#chboxProduct").change(function(){
		if($(".invoice-table.prod").css("display") == "block")
			$(".invoice-table.prod").css("display", "none");
		else
			$(".invoice-table.prod").css("display", "block");
	});
	$("#chboxService").change(function(){
		if($(".invoice-table.service").css("display") == "block")
			$(".invoice-table.service").css("display", "none");
		else
			$(".invoice-table.service").css("display", "block");
	});
	$( "table" ).each(function( index ) {
		  console.log( index + "tableeeeee");
		});
	$("#addService").click(function(){
		$.fn.addLine({targetClass: "service"});
	});
	$("#addProd").click(function(){
		$.fn.addLine({targetClass: "prod"});
	});
});